<?php
/**
 * Created by PhpStorm.
 * User: 老毛
 * Date: 2019/9/4
 * Time: 18:44
 */

namespace app\api\controller;


use think\Db;

class DataToken extends TokenBase
{
    //修改会员头像
   public function updateHeaderImage(){
       $file = $_FILES;
       if(!isset($file['file'])) return ajaxReturn('修改失败',999);
       $data = setFile($file['file']);
       //修改会员头像
       $dataResult = Db::name('php_member')->where(['id' => $this->uid])->update(['image' => $data]);
       if(!$dataResult) return ajaxReturn('修改失败',999);
       return ajaxReturn('修改成功',1,$data);
   }
   //修改昵称手机性别密码
    public function updateData(){
       $post = $_POST;
       if(!isset($post['cate'])) return ajaxReturn('修改失败',999);
       switch ($post['cate']){
           case 1:  //修改昵称
               if(!isset($post['nickname'])|| !$post['nickname']) return ajaxReturn('请完善你要修改的内容',999);
               $dataResult = Db::name('php_member')->where(['id' => $this->uid])->update(['nickname' => $post['nickname']]);
               break;
           case 2:  //修改手机
               if(!isset($post['phone'])|| !isset($post['code'])|| !$post['phone'] || !$post['code']) return ajaxReturn('请完善你要修改的内容',999);
               //验证手机号格式
               if(!preg_match('/^1[3-9][0-9]{9}/',$post['phone'])) return ajaxReturn('请输入正确的手机号',0);
               //判断验证码是否正确
               $codeCheack = Db::name('php_sms')->where(['phone' => $post['phone'],'code'=> $post['code'],'status'=>0])->find();
               if (!$codeCheack) return ajaxReturn('验证码不正确',99);
               Db::name('php_sms')->where(['id' => $codeCheack['id']])->update(['status' => 1]);
               //验证成功存入数据
               $dataResult = Db::name('php_member')->where(['id' => $this->uid])->update(['username' => $post['phone']]);
               break;
           case 3:  //修改性别
               if(!isset($post['sex'])) return ajaxReturn('请完善你要修改的内容',999);
               $dataResult = Db::name('php_member')->where(['id' => $this->uid])->update(['sex' => $post['sex']]);
               break;
           default:
               return ajaxReturn('修改失败',999);
       }
        if(!$dataResult) return ajaxReturn('修改失败',999);
        return ajaxReturn('修改成功',1);
    }
    //退出登录
    public function loginout(){
       //清除缓存
        $token = $this->request->header('api-token');
        $data = cache($token,null);
        if(!$data) return ajaxReturn('退出失败',99);
        return ajaxReturn('退出成功',1);
    }
    //发布圈子
    public function setquan(){
        $post = $_POST;
        //验证数据
        if(!$post['content']) return ajaxReturn('请输入内容！',99);
        //判断是否有图片
        if($post['image']){
            $post['image'] = str_replace(",","|",$post['image']);
        }
        $post['uid'] = $this->uid;
        $datas = Db::name('php_quanzhi')->insert($post);
        if(!$datas) return ajaxReturn('发布失败！',99);
        return ajaxReturn('发布成功',1);
    }
    //获取消息通知
    public function gettongzhi(){
        $data = Db::name('php_tongzhi')->where(['uid' => $this->uid])->order('status add')->select();
        if(!$data) return ajaxReturn('暂无消息！',99);
        return ajaxReturn('获取成功！',1,$data);
    }


}